<?php

namespace ajiho\helper;

use Exception;
use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Config;

class Sundry
{


    /**
     * 对一个字符串进行高亮操作
     * @param $keyword
     * @param $str
     * @param $color
     * @return array|string|string[]|null
     */
    public static function keywordHighlight($keyword, $str, $color = 'red')
    {
        return preg_replace("/($keyword)/i", "<span style='color: " . $color . "' ><b>\\1</b></span>", $str);
    }


    /**
     * 发送邮件
     * @param string $address 邮箱地址(接收人)
     * @param string $title 标题
     * @param string $body 内容
     * @return bool|Exception
     */
    public static function sendmail(string $address, string $title, string $body)
    {
        $mail = new PHPMailer(true);
        try {
            $mail->SMTPDebug = 0;
            $mail->isSMTP();
            $mail->CharSet = "UTF-8";
            $mail->Encoding = "base64";
            $mail->Host = Config::get("helper.sendmail.host");
            $mail->SMTPAuth = true;
            $mail->Username = Config::get("helper.sendmail.username");
            $mail->Password = Config::get("helper.sendmail.password");
            $mail->SMTPSecure = Config::get("helper.sendmail.encryption");
            $mail->Port = Config::get("helper.sendmail.port");
            //发件人
            $mail->setFrom(Config::get("helper.sendmail.from_address"), Config::get("helper.sendmail.from_name"));
            //接收人
            $mail->addAddress($address);
            //是否是html类型的邮箱
            $mail->isHTML(true);
            //防止中文乱码
            $mail->Subject = "=?UTF-8?B?" . base64_encode($title) . "?=";
            $mail->Body = $body;
            $mail->AltBody = '您的邮箱客户端不支持显示HTML内容';
            $mail->send();
            return true;
        } catch (Exception $e) {
            return $e;
        }
    }


    /**
     * 非exif拓展实现的exif_imagetype函数
     * @param $filename
     * @return false|mixed
     */
    public static function exif_imagetype($filename)
    {
        if ((list($width, $height, $type, $attr) = getimagesize($filename))) {
            return $type;
        }
        return false;
    }


}